//导入数据库连接模块

const db = require('../db/index');

const paperModel = db.model('paper', {
  __v: { type: Number, select: false },
  userid: { type: String, require: true },
  papername: { type: String, require: true },
  paperinfo: { type: Object, default: {} },
  answersid: { type: Array, default: [] },
  isPublish: { type: Boolean, default: false },
  ctime: { type: Date, default: Date.now },
  utime: { type: Date },
});

module.exports = {
  // 添加问卷
  createPaper(info) {
    // console.log(info);
    const paper = new paperModel(info);
    return paper
      .save()
      .then(res => res)
      .catch(err => {
        console.log('插入失败' + err);
        return false;
      });
  },
  getUserPapersCount({ searchField, searchValue }, ids) {
    const conditions = { _id: ids };
    if (searchField && searchValue) {
      conditions[searchField] = {
        $regex: new RegExp(`.*${searchValue}.*`, 'ig'),
      };
    }
    return paperModel
      .find(conditions)
      .then(res => {
        return res.length;
      })
      .catch(err => {
        console.log('查询失败' + err);
        return false;
      });
  },
  // 获取问卷
  getPapers({ page, searchField, searchValue }, ids) {
    const conditions = { _id: ids };
    if (searchField && searchValue) {
      conditions[searchField] = {
        $regex: new RegExp(`.*${searchValue}.*`, 'ig'),
      };
    }
    return paperModel
      .find(conditions)
      .skip((page - 1) * 9)
      .limit(9)
      .sort({ ctime: -1 })
      .then(res => {
        return res;
      })
      .catch(err => {
        console.log('查询失败' + err);
        return false;
      });
  },
  // 根据id获取单个问卷
  getPaperById(_id, info = {}) {
    // console.log(_id);
    return paperModel
      .findById(_id, info)
      .then(res => res)
      .catch(err => {
        console.log('查询失败' + err);
        return false;
      });
  },
  // 根据id删除单个问卷
  deletePaperById(_id) {
    return paperModel
      .deleteOne({ _id })
      .then(res => {
        return res;
      })
      .catch(err => {
        console.log('删除失败' + err);
        return false;
      });
  },
  // 根据多个ID批量删除问卷
  deletePapersByIds(ids) {
    return paperModel
      .deleteMany({ _id: { $in: ids } })
      .then(res => res)
      .catch(err => {
        console.log('删除失败' + err);
        return false;
      });
  },
  // 根据ID编辑问卷信息
  updatePaperById(_id, data) {
    // console.log(data);
    return paperModel
      .updateOne({ _id }, { $set: data, utime: Date.now() })
      .then(res => res)
      .catch(err => {
        console.log('修改失败' + err);
        return false;
      });
  },
  // 判断字段值是否存在
  isExist(field, value) {
    return paperModel
      .findOne({ [field]: value })
      .then(res => res)
      .catch(err => {
        console.log('查询失败' + err);
        return false;
      });
  },
  updatePaperAnswers(id, info) {
    return paperModel
      .updateOne({ _id: id }, info)
      .then(res => {
        return res;
      })
      .catch(err => {
        console.log('error:' + err.message);
        return false;
      });
  },
  getPaperAnswersid(id) {
    return paperModel
      .findById(id)
      .then(res => {
        return res.answersid;
      })
      .catch(err => {
        console.log('error:' + err.message);
        return false;
      });
  },
};
